package com.zxw.user.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zxw.common.core.base.res.BasePageRes;
import com.zxw.common.core.base.res.BaseRes;
import com.zxw.user.api.UserAPI;
import com.zxw.user.domain.dto.LoginUserDTO;
import com.zxw.user.domain.dto.UserDTO;
import com.zxw.user.domain.dto.UserUpdateDTO;
import com.zxw.user.domain.entity.User;
import com.zxw.user.domain.vo.*;
import com.zxw.user.service.LoginService;
import com.zxw.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Xuewei.Zhang
 * @since 2022-01-21
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController implements UserAPI {

    private final LoginService loginService;

    private final UserService userService;

    @ApiOperationSupport(author = "Xuewei.Zhang")
    @ApiOperation("获取的当前登陆用户的信息")
    @GetMapping("/login-user-Info")
    public BaseRes<LoginUserDTO> getLoginUserInfo() {
        LoginUserDTO loginUserDTO = loginService.getLoginUserInfo();
        return BaseRes.ok(loginUserDTO);
    }

    @ApiOperationSupport(author = "Xuewei.Zhang")
    @ApiOperation("用户列表")
    @GetMapping("/user-list")
    public BasePageRes<UserDTO> findUserList(UserPageVO userPageVO) {
        IPage<UserDTO> userDTOIPage = userService.getUserList(userPageVO);
        return BasePageRes.success(userDTOIPage);
    }

    @ApiOperationSupport(author = "Xuewei.Zhang")
    @ApiOperation("根据主键id获取用户信息")
    @GetMapping("/{userId}")
    public BaseRes<UserUpdateDTO> findUserById(Long userId) {
        UserUpdateDTO userUpdateDTO = userService.getUserById(userId);
        return BaseRes.ok(userUpdateDTO);
    }

    @ApiOperationSupport(author = "Xuewei.Zhang")
    @ApiOperation("添加用户")
    @PostMapping("/insert")
    public BaseRes<User> insertUser(@RequestBody UserAddVO userAddVO) {
        User user = userService.add(userAddVO);
        return BaseRes.ok(user);
    }

    @ApiOperationSupport(author = "Xuewei.Zhang")
    @ApiOperation("修改用户（角色、基本信息）")
    @PutMapping("/update/{userId}")
    public BaseRes updateUser(@PathVariable("userId") Long userId, @RequestBody UserUpdateVO userAddVO) {
        userAddVO.setId(userId);
        userService.updateBase(userAddVO);
        return BaseRes.ok();
    }

    @ApiOperationSupport(author = "Xuewei.Zhang")
    @ApiOperation("删除用户")
    @DeleteMapping("/{userIds}")
    public BaseRes deleteUser(@PathVariable("userIds") String userIds) {
        userService.delete(userIds);
        return BaseRes.okDelete();
    }

    @ApiOperationSupport(author = "Xuewei.Zhang")
    @ApiOperation("修改用户（密码、状态）")
    @PatchMapping("/update/part/{userId}")
    public BaseRes updatePart(@PathVariable("userId") Long userId, User user) {
        user.setId(userId);
        userService.updatePart(user);
        return BaseRes.okUpdate();
    }

    @ApiOperationSupport(author = "Xuewei.Zhang")
    @ApiOperation("重置用户密码")
    @PatchMapping("/update/part/password/{account}/{code}")
    public BaseRes resetPassword(@PathVariable("account") String account, @PathVariable("code") String code) {
        userService.resetPassword(account, code);
        return BaseRes.okUpdate();
    }

    @ApiOperationSupport(author = "Xuewei.Zhang")
    @ApiOperation("修改用户密码")
    @PatchMapping("/update/password")
    public BaseRes updatePassword(@Validated @RequestBody UserUpPassVO userUpPassVO) {
        userService.updatePassword(userUpPassVO);
        return BaseRes.okUpdate();
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", defaultValue = "1263944101@qq.com", value = "账号", required = true),
            @ApiImplicitParam(name = "passwordCode", defaultValue = "pass", value = "密码", required = true),
    })
    @ApiOperationSupport(author = "Xuewei.Zhang")
    @ApiOperation("注册用户（流程图业务）")
    @Override
    public BaseRes registerFromFlow(@RequestBody RegisterUserVO user) {
        String msg = userService.registerFromFlow(user);
        return BaseRes.ok(msg);
    }


}
